<?php
ini_set("session.name","session");
date_default_timezone_set("PRC");
error_reporting(E_ALL);
ini_set("display_error","Off");
define("PATH_SRC_PHP",realpath(__DIR__."/../"));
define("PATH_PROJECT",realpath(__DIR__."/../../../"));
define("PATH_SRC_DATA",PATH_SRC_PHP."/data");
define("PATH_SRC_PHP_LIB",PATH_SRC_PHP."/lib");

function ptClassLoader($class)
{
    if(strpos($class,'pt') === 0){
        $path = str_replace('\\', DIRECTORY_SEPARATOR, $class);
        $file = __DIR__ . DIRECTORY_SEPARATOR . $path . '.php';
        if (file_exists($file)) {
            require_once($file);
        }
    }
}
spl_autoload_register('ptClassLoader');
require PATH_SRC_PHP_LIB.'/spyc/Spyc.php';
require __DIR__.'/pt/functions.php';
require __DIR__.'/pt/db/Mysql.php';
class App{
    static $apiUnlessPaths = array();
    static $config = array();
    static $mode = "development";
    static function getDbConfig(){
        $phinx_config = array();
        if(is_file(PATH_SRC_PHP."/phinx.yml")){
            $phinx_config = $array = Spyc::YAMLLoad(@file_get_contents(PATH_SRC_PHP."/phinx.yml"));
        }elseif(is_file(PATH_PROJECT."/phinx.yml")){
            $phinx_config = Spyc::YAMLLoad(@file_get_contents(PATH_PROJECT."/phinx.yml"));
        }
        $db_config = array();
        if($phinx_config){
            $db_config = $phinx_config['environments'][self::$mode];
            self::$config['db'] = array(
                'default'=>array(
                    'type'=>$db_config['adapter'],
                    'host'=>$db_config['host'],
                    'port'=>$db_config['port'],
                    'dbname'=>$db_config['name'],
                    'dbuser'=>$db_config['user'],
                    'dbpass'=>$db_config['pass'],
                    'charset'=>$db_config['charset'],
                    'prefix'=>empty($db_config['table_prefix']) ? "":$db_config['table_prefix'],
                )
            );
        }
        return $db_config;
    }
}

include __DIR__."/../config/setting.php";

function checkAuth(){
    return isset($_SESSION['username']) && $_SESSION['username'] == \App::$config["admin"]['username'];
}
